package com.dzu.gulimall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dzu.common.utils.PageUtils;
import com.dzu.common.utils.Query;
import com.dzu.gulimall.order.dao.OrderItemDao;
import com.dzu.gulimall.order.entity.OrderItemEntity;
import com.dzu.gulimall.order.entity.OrderReturnReasonEntity;
import com.dzu.gulimall.order.service.OrderItemService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

//@RabbitListener(queues = {"gulimall_queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues 声明所有需要监听的队列
     *
     * @param message org.springframework.amqp.core  rabbitmq原生消息
     * @param entity  boot自动封装成entity，发布的对象和接受的对象一致
     * @param channel 当前所使用传输数据的通道
     *                <p>
     *                Queue：可以很多人监听，只要收到消息，队列删除消息，而且只能有一个收到此消息
     *                <p>
     *                场景：
     *                1、订单服务有多个：：同一个消息，只能被一个客户端消费 、
     *                2、只有一个消息处理完，方法运行结束，我们才能接受下一个消息
     */
//    @RabbitListener(queues = {"gulimall_queue"})
//    @RabbitHandler
    public void receiveMessage(Message message, OrderReturnReasonEntity entity, Channel channel) {
        System.out.println("OrderReturnReasonEntity::" + entity + "message::>" + message);
        // channel内 按顺序自增的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            channel.basicAck(deliveryTag, false);
            // 拒接消息，false 丢弃，true 重回队列
//            channel.basicReject(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    @RabbitHandler
    public void receiveMessage(Message message, OrderItemEntity entity, Channel channel) {
        System.out.println("OrderItemEntity::" + entity + "message::>" + message);
    }

//    @RabbitHandler
    public void receiveMessage(Message message, byte[] bytes, Channel channel) {
        //2021-12-19 21:30:10.304  WARN 19584 --- [ntContainer#0-1] o.s.a.s.c.Jackson2JsonMessageConverter   : Could
        // not convert incoming message with content-type [null], 'json' keyword missing.
        //Message::123的风格
        System.out.println("Message::" + new String(bytes));
    }

}
